package com.moss.cloud.base.biz.controller;

import com.moss.cloud.base.api.model.from.*;
import com.moss.cloud.base.api.model.param.UserPageParam;
import com.moss.cloud.base.biz.service.ISysUserService;
import com.moss.cloud.common.base.model.po.SysUser;
import com.moss.cloud.common.core.constant.ServiceList;
import com.moss.cloud.common.core.model.Result;
import com.moss.cloud.common.core.utils.UserContextHolder;
import com.moss.cloud.common.security.annotation.SysLog;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户管理前端控制器
 *
 * @author 瑾年
 * @date 2023年3月23日
 */
@RestController
@RequestMapping(ServiceList.BASE + "/sysUser")
public class SysUserController {
    private final ISysUserService sysUserService;

    public SysUserController(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 保存用户信息--通用用户注册
     *
     * @param sysUserForm
     * @return
     * @throws Exception
     */
    @PostMapping("/saveUser")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "保存用户信息--通用用户注册")
    public Result<?> saveUser(@RequestBody @Validated SysUserForm sysUserForm) throws Exception {
        return Result.success(sysUserService.saveUser(sysUserForm));
    }

    /**
     * 删除用户信息
     *
     * @param preUserFrom
     * @return
     */
    @PostMapping("/removeUser")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "删除用户信息")
    public Result<?> removeUser(@RequestBody @Validated PreUserFrom preUserFrom) {
        return Result.success(sysUserService.removeUser(preUserFrom));
    }

    /**
     * 手动锁定账号
     *
     * @param userLockFrom
     * @return
     */
    @PostMapping("/lockAccount")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "手动锁定账号")
    public Result<?> lockAccount(@RequestBody @Validated UserLockFrom userLockFrom) {
        return Result.success(sysUserService.lockAccount(userLockFrom));
    }

    /**
     * 手动解锁账号
     *
     * @param userUnlockFrom
     * @return
     */
    @PostMapping("/unlockAccount")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "手动解锁账号")
    public Result<?> unlockAccount(@RequestBody @Validated UserUnlockFrom userUnlockFrom) {
        return Result.success(sysUserService.unlockAccount(userUnlockFrom));
    }

    /**
     * 分页查询用户信息
     *
     * @param userPageParam
     * @return
     */
    @PostMapping("/userPage")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "分页查询用户信息")
    public Result<?> userPage(@RequestBody @Validated UserPageParam userPageParam) {
        return Result.success(sysUserService.userPage(userPageParam));
    }

    /**
     * 管理员添加用户
     *
     * @param sysUserForm
     * @return
     * @throws Exception
     */
    @PostMapping("/adminAddUser")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "企业管理员添加用户")
    public Result<?> adminAddUser(@RequestBody @Validated SysUserForm sysUserForm) throws Exception {
        return Result.success(sysUserService.adminAddUser(sysUserForm));
    }

    /**
     * 管理员更新用户
     *
     * @param sysUserForm
     * @return
     * @throws Exception
     */
    @PostMapping("/adminUpdUser")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "企业管理员更新用户")
    public Result<?> adminUpdUser(@RequestBody @Validated SysUserForm sysUserForm) throws Exception {
        return Result.success(sysUserService.adminUpdateUser(sysUserForm));
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @PostMapping("/userInfo")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "获取当前登录用户信息")
    public Result<?> userInfo() {
        return Result.success(sysUserService.userInfo());
    }

    /**
     * 根据用户ID查询用户信息
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/usersById")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "查询用户信息")
    public Result<?> usersById(@RequestBody @Validated BaseFrom baseFrom) {
        SysUser sysUser = sysUserService.findByUserId(baseFrom.getId());
        sysUser.setPassword("");
        return Result.success(sysUser);
    }

    /**
     * 搜索用户
     *
     * @param userPageParam
     * @return
     */
    @PostMapping("/searchUser")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "搜索用户")
    public Result<?> searchUser(@RequestBody UserPageParam userPageParam) {
        return Result.success(sysUserService.searchUser(userPageParam));
    }

    /**
     * 重置用户密码
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/resetPassword")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "重置用户密码")
    public Result<?> resetPassword(@RequestBody @Validated BaseFrom baseFrom) throws Exception {
        return Result.success(sysUserService.resetPassword(baseFrom));
    }

    /**
     * 用户修改密码
     *
     * @param updatePasswordFrom
     * @return
     */
    @PostMapping("/updatePassword")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "用户修改密码")
    public Result<?> updatePassword(@RequestBody @Validated UpdatePasswordFrom updatePasswordFrom) throws Exception {
        return Result.success(sysUserService.updatePassword(updatePasswordFrom));
    }

    /**
     * 用户更新基本信息
     *
     * @param updateUserForm
     * @return
     */
    @PostMapping("/updateSelf")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "用户更新基本信息")
    public Result<?> updateSelf(@RequestBody @Validated UpdateUserForm updateUserForm) {
        return Result.success(sysUserService.updateSelf(updateUserForm));
    }

    /**
     * 当前用户信息
     *
     * @return
     */
    @PostMapping("/currentUser")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "当前用户信息")
    public Result<?> currentUser() {
        SysUser sysUser = sysUserService.findByUserId(UserContextHolder.getUserId());
        sysUser.setPassword("");
        return Result.success(sysUser);
    }

    /**
     * 员工离职
     *
     * @param baseFrom
     * @return
     */
    @PostMapping("/userDepart")
    @SysLog(serverName = ServiceList.MOSS_CLOUD_BASE, operation = "员工离职")
    public Result<?> userDepart(@RequestBody @Validated BaseFrom baseFrom){
        return Result.success(sysUserService.userDepart(baseFrom));
    }
}
